package dhz.tools.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import dhz.tools.dialog.GzipDialog;
import dhz.tools.util.IOUtils;

public class GzipService {

	/**
	 * 递归对指定文件夹(文件)进行GZIP压缩
	 * @param shell
	 * @param file
	 * @param gzipStr
	 * @param nameTexts	只压缩的扩展名，空表示全部压缩
	 * @param type
	 * @param sizeFromVal
	 * @param sizeToVal
	 * @return	返回true:继续递归，false:结束整个递归
	 * @throws Exception
	 */
	public boolean gzipFile(Shell shell,File file,String gzipStr,String[] nameTexts,int type,int sizeFromVal,int sizeToVal) throws Exception{
		if(file.isDirectory()){
			for(File subFile : file.listFiles()){
				if(!gzipFile(shell, subFile, gzipStr, nameTexts, type, sizeFromVal, sizeToVal)){
					return false;
				}
			}
			return true;
		}
		if(!gzipStr.startsWith(".")){
			gzipStr = "." + gzipStr;
		}
		//大小判断
		long size = file.length();
		if(sizeFromVal > 0){
			if(size < sizeFromVal){
				return true;
			}
		}
		if(sizeToVal > 0){
			if(size > sizeToVal){
				return true;
			}
		}
		//跳过压缩文件及指定文件
		String[] disabled = {gzipStr,".class",".mf",".jar",".xml",".tld",".db"};
		String name = file.getName().toLowerCase();
		for(String f : disabled){
			if(name.endsWith(f)){
				return true;
			}
		}
		File outFile = new File(file.getAbsolutePath()+gzipStr);
		//新文件已经存在处理
		if(outFile.exists()){
			if(type == GzipDialog.TYPE_SKIP){
				return true;
			}
			if(type == GzipDialog.TYPE_INQUIRE){
				MessageBox msgBox = new MessageBox(shell,SWT.YES|SWT.NO|SWT.CANCEL|MessageDialog.QUESTION);
				msgBox.setText("请确认");
				msgBox.setMessage("文件["+outFile.getAbsolutePath()+"]已经存在，是否覆盖？");
				int result = msgBox.open();
				if(result == SWT.NO){
					return true;
				}else if(result == SWT.CANCEL){
					return false;
				}
			}
		}
		//如果指定了文件类型，则只压缩指定类型
		if(nameTexts != null){
			boolean flag = false;
			for(String fName : nameTexts){
				if(name.endsWith(fName)){
					flag = true;
					break;
				}
			}
			if(!flag){
				return true;
			}
		}
		gzipFile(file, outFile);
		return true;
	}
	
	/*
	 * 压缩文件
	 * @param file
	 * @param outFile
	 * @throws Exception
	 */
	private void gzipFile(File file,File outFile) throws Exception{
		InputStream is = null;
		OutputStream os = null;
		GZIPOutputStream zipOs = null;
		try {
			is = new FileInputStream(file);
			os = new FileOutputStream(outFile);
			zipOs = new GZIPOutputStream(os);
			
			byte[] buf = new byte[1024];
			int len;
			while((len = is.read(buf)) != -1){
				zipOs.write(buf,0,len);
			}
			zipOs.finish();
		} finally {
			IOUtils.closeOs(zipOs);
			IOUtils.closeOs(os);
			IOUtils.closeIs(is);
		}
	}
}
